home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d13 / perl3041.arc / PERLDB.PL < prev    next >
Perl Script  |  1990-11-19  |  14KB  |  536 lines

  1. package DB;
  2.  
  3. $header = '$Header: perldb.pl,v 3.0.1.5 90/11/10 01:40:26 lwall Locked $';
  4. #
  5. # This file is automatically included if you do perl -d.
  6. # It's probably not useful to include this yourself.
  7. #
  8. # Perl supplies the values for @line and %sub.  It effectively inserts
  9. # a do DB'DB(<linenum>); in front of every place that can
  10. # have a breakpoint.  It also inserts a do 'perldb.pl' before the first line.
  11. #
  12. # $Log:    perldb.pl,v $
  13. # Revision 3.0.1.5  90/11/10  01:40:26  lwall
  14. # patch38: the debugger wouldn't stop correctly or do action routines
  15. # Revision 3.0.1.4  90/10/15  17:40:38  lwall
  16. # patch29: added caller
  17. # patch29: the debugger now understands packages and evals
  18. # patch29: scripts now run at almost full speed under the debugger
  19. # patch29: more variables are settable from debugger
  20. # Revision 3.0.1.3  90/08/09  04:00:58  lwall
  21. # patch19: debugger now allows continuation lines
  22. # patch19: debugger can now dump lists of variables
  23. # patch19: debugger can now add aliases easily from prompt
  24. # Revision 3.0.1.2  90/03/12  16:39:39  lwall
  25. # patch13: perl -d didn't format stack traces of *foo right
  26. # patch13: perl -d wiped out scalar return values of subroutines
  27. # Revision 3.0.1.1  89/10/26  23:14:02  lwall
  28. # patch1: RCS expanded an unintended $Header in lib/perldb.pl
  29. # Revision 3.0  89/10/18  15:19:46  lwall
  30. # 3.0 baseline
  31. # Revision 2.0  88/06/05  00:09:45  root
  32. # Baseline version 2.0.
  33. #
  34.  
  35. # MS-DOS version uses CON instead of /dev/tty
  36.  
  37. open(IN, "<CON") || open(IN,  "<&STDIN");    # so we don't dingle stdin
  38. open(OUT,">CON") || open(OUT, ">&STDOUT");    # so we don't dongle stdout
  39. select(OUT);
  40. $| = 1;                # for DB'OUT
  41. select(STDOUT);
  42. $| = 1;                # for real STDOUT
  43. $sub = '';
  44.  
  45. $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
  46. print OUT "\nLoading DB routines from $header\n\nEnter h for help.\n\n";
  47.  
  48. sub DB {
  49.     &save;
  50.     ($package, $filename, $line) = caller;
  51.     $usercontext = '($@, $!, $[, $,, $/, $\) = @saved;' .
  52.     "package $package;";        # this won't let them modify, alas
  53.     local(*dbline) = "_<$filename";
  54.     $max = $#dbline;
  55.     if (($stop,$action) = split(/\0/,$dbline{$line})) {
  56.     if ($stop eq '1') {
  57.         $signal |= 1;
  58.     }
  59.     else {
  60.         &eval("\$DB'signal |= do {$stop;}");
  61.         $dbline{$line} =~ s/;9($|\0)/$1/;
  62.     }
  63.     }
  64.     if ($single || $trace || $signal) {
  65.     print OUT "$package'" unless $sub =~ /'/;
  66.     print OUT "$sub($filename:$line):\t",$dbline[$line];
  67.     for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
  68.         last if $dbline[$i] =~ /^\s*(}|#|\n)/;
  69.         print OUT "$sub($filename:$i):\t",$dbline[$i];
  70.     }
  71.     }
  72.     &eval($action) if $action;
  73.     if ($single || $signal) {
  74.     &eval($pre) if $pre;
  75.     print OUT $#stack . " levels deep in subroutine calls!\n"
  76.         if $single & 4;
  77.     $start = $line;
  78.     while ((print OUT "  DB<", $#hist+1, "> "), $cmd=&gets) {
  79.         $single = 0;
  80.         $signal = 0;
  81.         $cmd eq '' && exit 0;
  82.         chop($cmd);
  83.         $cmd =~ s/\\$// && do {
  84.         print OUT "  cont: ";
  85.         $cmd .= &gets;
  86.         redo;
  87.         };
  88.         $cmd =~ /^q$/ && exit 0;
  89.         $cmd =~ /^$/ && ($cmd = $laststep);
  90.         push(@hist,$cmd) if length($cmd) > 1;
  91.         ($i) = split(/\s+/,$cmd);
  92.         eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
  93.         $cmd =~ /^h$/ && do {
  94.         print OUT "
  95. T        Stack trace.
  96. s        Single step.
  97. n        Next, steps over subroutine calls.
  98. r        Return from current subroutine.
  99. c [line]    Continue; optionally inserts a one-time-only breakpoint 
  100.         at the specified line.
  101. <CR>        Repeat last n or s.
  102. l min+incr    List incr+1 lines starting at min.
  103. l min-max    List lines.
  104. l line        List line;
  105. l        List next window.
  106. -        List previous window.
  107. w line        List window around line.
  108. l subname    List subroutine.
  109. f filename    Switch to filename.
  110. /pattern/    Search forwards for pattern; final / is optional.
  111. ?pattern?    Search backwards for pattern.
  112. L        List breakpoints and actions.
  113. S        List subroutine names.
  114. t        Toggle trace mode.
  115. b [line] [condition]
  116.         Set breakpoint; line defaults to the current execution line; 
  117.         condition breaks if it evaluates to true, defaults to \'1\'.
  118. b subname [condition]
  119.         Set breakpoint at first line of subroutine.
  120. d [line]    Delete breakpoint.
  121. D        Delete all breakpoints.
  122. a [line] command
  123.         Set an action to be done before the line is executed.
  124.         Sequence is: check for breakpoint, print line if necessary,
  125.         do action, prompt user if breakpoint or step, evaluate line.
  126. A        Delete all actions.
  127. V [pkg [vars]]    List some (default all) variables in package (default current).
  128. X [vars]    Same as \"V currentpackage [vars]\".
  129. < command    Define command before prompt.
  130. > command    Define command after prompt.
  131. ! number    Redo command (default previous command).
  132. ! -number    Redo number\'th to last command.
  133. H -number    Display last number commands (default all).
  134. q or ^D        Quit.
  135. p expr        Same as \"print DB'OUT expr\" in current package.
  136. = [alias value]    Define a command alias, or list current aliases.
  137. command        Execute as a perl statement in current package.
  138.  
  139. ";
  140.         next; };
  141.         $cmd =~ /^t$/ && do {
  142.         $trace = !$trace;
  143.         print OUT "Trace = ".($trace?"on":"off")."\n";
  144.         next; };
  145.         $cmd =~ /^S$/ && do {
  146.         foreach $subname (sort(keys %sub)) {
  147.             print OUT $subname,"\n";
  148.         }
  149.         next; };
  150.         $cmd =~ s/^X\b/V $package/;
  151.         $cmd =~ /^V$/ && do {
  152.         $cmd = 'V $package'; };
  153.         $cmd =~ /^V\s*(\S+)\s*(.*)/ && do {
  154.         $packname = $1;
  155.         @vars = split(' ',$2);
  156.         do 'dumpvar.pl' unless defined &main'dumpvar;
  157.         if (defined &main'dumpvar) {
  158.             &main'dumpvar($packname,@vars);
  159.         }
  160.         else {
  161.             print DB'OUT "dumpvar.pl not available.\n";
  162.         }
  163.         next; };
  164.         $cmd =~ /^f\s*(.*)/ && do {
  165.         $file = $1;
  166.         if (!$file) {
  167.             print OUT "The old f command is now the r command.\n";
  168.             print OUT "The new f command switches filenames.\n";
  169.             next;
  170.         }
  171.         if (!defined $_main{'_<' . $file}) {
  172.             if (($try) = grep(m#^_<.*$file#, keys %_main)) {
  173.             $file = substr($try,2);
  174.             print "\n$file:\n";
  175.             }
  176.         }
  177.         if (!defined $_main{'_<' . $file}) {
  178.             print OUT "There's no code here anything matching $file.\n";
  179.             next;
  180.         }
  181.         elsif ($file ne $filename) {
  182.             *dbline = "_<$file";
  183.             $max = $#dbline;
  184.             $filename = $file;
  185.             $start = 1;
  186.             $cmd = "l";
  187.         } };
  188.         $cmd =~ /^l\s*(['A-Za-z_]['\w]*)/ && do {
  189.         $subname = $1;
  190.         $subname = "main'" . $subname unless $subname =~ /'/;
  191.         $subname = "main" . $subname if substr($subname,0,1) eq "'";
  192.         ($file,$subrange) = split(/:/,$sub{$subname});
  193.         if ($file ne $filename) {
  194.             *dbline = "_<$file";
  195.             $max = $#dbline;
  196.             $filename = $file;
  197.         }
  198.         if ($subrange) {
  199.             if (eval($subrange) < -$window) {
  200.             $subrange =~ s/-.*/+/;
  201.             }
  202.             $cmd = "l $subrange";
  203.         } else {
  204.             print OUT "Subroutine $1 not found.\n";
  205.             next;
  206.         } };
  207.         $cmd =~ /^w\s*(\d*)$/ && do {
  208.         $incr = $window - 1;
  209.         $start = $1 if $1;
  210.         $start -= $preview;
  211.         $cmd = 'l ' . $start . '-' . ($start + $incr); };
  212.         $cmd =~ /^-$/ && do {
  213.         $incr = $window - 1;
  214.         $cmd = 'l ' . ($start-$window*2) . '+'; };
  215.         $cmd =~ /^l$/ && do {
  216.         $incr = $window - 1;
  217.         $cmd = 'l ' . $start . '-' . ($start + $incr); };
  218.         $cmd =~ /^l\s*(\d*)\+(\d*)$/ && do {
  219.         $start = $1 if $1;
  220.         $incr = $2;
  221.         $incr = $window - 1 unless $incr;
  222.         $cmd = 'l ' . $start . '-' . ($start + $incr); };
  223.         $cmd =~ /^l\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
  224.         $end = (!$2) ? $max : ($4 ? $4 : $2);
  225.         $end = $max if $end > $max;
  226.         $i = $2;
  227.         $i = $line if $i eq '.';
  228.         $i = 1 if $i < 1;
  229.         for (; $i <= $end; $i++) {
  230.             print OUT "$i:\t", $dbline[$i];
  231.             last if $signal;
  232.         }
  233.         $start = $i;    # remember in case they want more
  234.         $start = $max if $start > $max;
  235.         next; };
  236.         $cmd =~ /^D$/ && do {
  237.         print OUT "Deleting all breakpoints...\n";
  238.         for ($i = 1; $i <= $max ; $i++) {
  239.             if (defined $dbline{$i}) {
  240.             $dbline{$i} =~ s/^[^\0]+//;
  241.             if ($dbline{$i} =~ s/^\0?$//) {
  242.                 delete $dbline{$i};
  243.             }
  244.             }
  245.         }
  246.         next; };
  247.         $cmd =~ /^L$/ && do {
  248.         for ($i = 1; $i <= $max; $i++) {
  249.             if (defined $dbline{$i}) {
  250.             print OUT "$i:\t", $dbline[$i];
  251.             ($stop,$action) = split(/\0/, $dbline{$i});
  252.             print OUT "  break if (", $stop, ")\n" 
  253.                 if $stop;
  254.             print OUT "  action:  ", $action, "\n" 
  255.                 if $action;
  256.             last if $signal;
  257.             }
  258.         }
  259.         next; };
  260.         $cmd =~ /^b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
  261.         $subname = $1;
  262.         $cond = $2 || '1';
  263.         $subname = "$package'" . $subname unless $subname =~ /'/;
  264.         $subname = "main" . $subname if substr($subname,0,1) eq "'";
  265.         ($filename,$i) = split(/[:-]/, $sub{$subname});
  266.         if ($i) {
  267.             *dbline = "_<$filename";
  268.             ++$i while $dbline[$i] == 0 && $i < $#dbline;
  269.             $dbline{$i} =~ s/^[^\0]*/$cond/;
  270.         } else {
  271.             print OUT "Subroutine $subname not found.\n";
  272.         }
  273.         next; };
  274.         $cmd =~ /^b\s*(\d*)\s*(.*)/ && do {
  275.         $i = ($1?$1:$line);
  276.         $cond = $2 || '1';
  277.         if ($dbline[$i] == 0) {
  278.             print OUT "Line $i not breakable.\n";
  279.         } else {
  280.             $dbline{$i} =~ s/^[^\0]*/$cond/;
  281.         }
  282.         next; };
  283.         $cmd =~ /^d\s*(\d+)?/ && do {
  284.         $i = ($1?$1:$line);
  285.         $dbline{$i} =~ s/^[^\0]*//;
  286.         delete $dbline{$i} if $dbline{$i} eq '';
  287.         next; };
  288.         $cmd =~ /^A$/ && do {
  289.         for ($i = 1; $i <= $max ; $i++) {
  290.             if (defined $dbline{$i}) {
  291.             $dbline{$i} =~ s/\0[^\0]*//;
  292.             delete $dbline{$i} if $dbline{$i} eq '';
  293.             }
  294.         }
  295.         next; };
  296.         $cmd =~ /^<\s*(.*)/ && do {
  297.         $pre = do action($1);
  298.         next; };
  299.         $cmd =~ /^>\s*(.*)/ && do {
  300.         $post = do action($1);
  301.         next; };
  302.         $cmd =~ /^a\s*(\d+)(\s+(.*))?/ && do {
  303.         $i = $1;
  304.         if ($dbline[$i] == 0) {
  305.             print OUT "Line $i may not have an action.\n";
  306.         } else {
  307.             $dbline{$i} =~ s/\0[^\0]*//;
  308.             $dbline{$i} .= "\0" . do action($3);
  309.         }
  310.         next; };
  311.         $cmd =~ /^n$/ && do {
  312.         $single = 2;
  313.         $laststep = $cmd;
  314.         last; };
  315.         $cmd =~ /^s$/ && do {
  316.         $single = 1;
  317.         $laststep = $cmd;
  318.         last; };
  319.         $cmd =~ /^c\s*(\d*)\s*$/ && do {
  320.         $i = $1;
  321.         if ($i) {
  322.             if ($dbline[$i] == 0) {
  323.                 print OUT "Line $i not breakable.\n";
  324.             next;
  325.             }
  326.             $dbline{$i} =~ s/(\0|$)/;9$1/;    # add one-time-only b.p.
  327.         }
  328.         for ($i=0; $i <= $#stack; ) {
  329.             $stack[$i++] &= ~1;
  330.         }
  331.         last; };
  332.         $cmd =~ /^r$/ && do {
  333.         $stack[$#stack] |= 2;
  334.         last; };
  335.         $cmd =~ /^T$/ && do {
  336.         local($p,$f,$l,$s,$h,$a,@a,@sub);
  337.         for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  338.             @a = @args;
  339.             for (@a) {
  340.             if (/^StB\000/ && length($_) == length($_main{'_main'})) {
  341.                 $_ = sprintf("%s",$_);
  342.             }
  343.             else {
  344.                 s/'/\\'/g;
  345.                 s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
  346.                 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  347.                 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  348.             }
  349.             }
  350.             $w = $w ? '@ = ' : '$ = ';
  351.             $a = $h ? '(' . join(', ', @a) . ')' : '';
  352.             push(@sub, "$w&$s$a from file $f line $l\n");
  353.             last if $signal;
  354.         }
  355.         for ($i=0; $i <= $#sub; $i++) {
  356.             last if $signal;
  357.             print OUT $sub[$i];
  358.         }
  359.             next; };
  360.         $cmd =~ /^\/(.*)$/ && do {
  361.         $inpat = $1;
  362.         $inpat =~ s:([^\\])/$:$1:;
  363.         if ($inpat ne "") {
  364.             eval '$inpat =~ m'."\n$inpat\n";    
  365.             if ($@ ne "") {
  366.                 print OUT "$@";
  367.                 next;
  368.             }
  369.             $pat = $inpat;
  370.         }
  371.         $end = $start;
  372.         eval '
  373.         for (;;) {
  374.             ++$start;
  375.             $start = 1 if ($start > $max);
  376.             last if ($start == $end);
  377.             if ($dbline[$start] =~ m'."\n$pat\n".'i) {
  378.             print OUT "$start:\t", $dbline[$start], "\n";
  379.             last;
  380.             }
  381.         } ';
  382.         print OUT "/$pat/: not found\n" if ($start == $end);
  383.         next; };
  384.         $cmd =~ /^\?(.*)$/ && do {
  385.         $inpat = $1;
  386.         $inpat =~ s:([^\\])\?$:$1:;
  387.         if ($inpat ne "") {
  388.             eval '$inpat =~ m'."\n$inpat\n";    
  389.             if ($@ ne "") {
  390.                 print OUT "$@";
  391.                 next;
  392.             }
  393.             $pat = $inpat;
  394.         }
  395.         $end = $start;
  396.         eval '
  397.         for (;;) {
  398.             --$start;
  399.             $start = $max if ($start <= 0);
  400.             last if ($start == $end);
  401.             if ($dbline[$start] =~ m'."\n$pat\n".'i) {
  402.             print OUT "$start:\t", $dbline[$start], "\n";
  403.             last;
  404.             }
  405.         } ';
  406.         print OUT "?$pat?: not found\n" if ($start == $end);
  407.         next; };
  408.         $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
  409.         pop(@hist) if length($cmd) > 1;
  410.         $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
  411.         $cmd = $hist[$i] . "\n";
  412.         print OUT $cmd;
  413.         redo; };
  414.         $cmd =~ /^!(.+)$/ && do {
  415.         $pat = "^$1";
  416.         pop(@hist) if length($cmd) > 1;
  417.         for ($i = $#hist; $i; --$i) {
  418.             last if $hist[$i] =~ $pat;
  419.         }
  420.         if (!$i) {
  421.             print OUT "No such command!\n\n";
  422.             next;
  423.         }
  424.         $cmd = $hist[$i] . "\n";
  425.         print OUT $cmd;
  426.         redo; };
  427.         $cmd =~ /^H\s*(-(\d+))?/ && do {
  428.         $end = $2?($#hist-$2):0;
  429.         $hist = 0 if $hist < 0;
  430.         for ($i=$#hist; $i>$end; $i--) {
  431.             print OUT "$i: ",$hist[$i],"\n"
  432.             unless $hist[$i] =~ /^.?$/;
  433.         };
  434.         next; };
  435.         $cmd =~ s/^p( .*)?$/print DB'OUT$1/;
  436.         $cmd =~ /^=/ && do {
  437.         if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
  438.             $alias{$k}="s~$k~$v~";
  439.             print OUT "$k = $v\n";
  440.         } elsif ($cmd =~ /^=\s*$/) {
  441.             foreach $k (sort keys(%alias)) {
  442.             if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
  443.                 print OUT "$k = $v\n";
  444.             } else {
  445.                 print OUT "$k\t$alias{$k}\n";
  446.             };
  447.             };
  448.         };
  449.         next; };
  450.         &eval($cmd);
  451.         print OUT "\n";
  452.     }
  453.     if ($post) {
  454.         &eval($post);
  455.     }
  456.     }
  457.     ($@, $!, $[, $,, $/, $\) = @saved;
  458. }
  459.  
  460. sub save {
  461.     @saved = ($@, $!, $[, $,, $/, $\);
  462.     $[ = 0; $, = ""; $/ = "\n"; $\ = "";
  463. }
  464.  
  465. sub eval {
  466.     eval "$usercontext $_[0]; &DB'save";
  467.     print OUT $@;
  468. }
  469.  
  470. sub action {
  471.     local($action) = @_;
  472.     while ($action =~ s/\\$//) {
  473.     print OUT "+ ";
  474.     $action .= &gets;
  475.     }
  476.     $action;
  477. }
  478.  
  479. sub gets {
  480.     local($.);
  481.     <IN>;
  482. }
  483.  
  484. sub catch {
  485.     $signal = 1;
  486. }
  487.  
  488. sub sub {
  489.     push(@stack, $single);
  490.     $single &= 1;
  491.     $single |= 4 if $#stack == $deep;
  492.     if (wantarray) {
  493.     @i = &$sub;
  494.     $single |= pop(@stack);
  495.     @i;
  496.     }
  497.     else {
  498.     $i = &$sub;
  499.     $single |= pop(@stack);
  500.     $i;
  501.     }
  502. }
  503.  
  504. $single = 1;            # so it stops on first executable statement
  505. @hist = ('?');
  506. $SIG{'INT'} = "DB'catch";
  507. $deep = 100;        # warning if stack gets this deep
  508. $window = 10;
  509. $preview = 3;
  510.  
  511. @stack = (0);
  512. @ARGS = @ARGV;
  513. for (@args) {
  514.     s/'/\\'/g;
  515.     s/(.*)/'$1'/ unless /^-?[\d.]+$/;
  516. }
  517.  
  518. if (-f '.perldb') {
  519.     do './.perldb';
  520. }
  521. elsif (-f "$ENV{'LOGDIR'}/.perldb") {
  522.     do "$ENV{'LOGDIR'}/.perldb";
  523. }
  524. elsif (-f "$ENV{'HOME'}/.perldb") {
  525.     do "$ENV{'HOME'}/.perldb";
  526. }
  527.  
  528. 1;
  529.